package day3.homework;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/*User -->手机号,密码,姓名
  完成一个XXXX用户管理系统
  一级菜单
  1. 注册功能-->随机生成9个  150开头的手机号   供选择
  2. 登录功能-->登录成功进入二级菜单
  3. 退出系统-->退出程序
  二级菜单
  1. 展示用户信息-->编号(根据下标+1)  手机号  密码  姓名
  2. 删除用户信息-->根据编号 删除账号   本机无法删除
  3. 退出登录-->退出到一级菜单*/
public class UserSysWork {
    static Scanner sc = new Scanner(System.in);
    //数组用于存放用户数据
    static List<User> users = new ArrayList<>();
    //定义当前登录用户的索引
    static int userIndex = -1;
    //静态读取数据
    static { input(); }
    public static void main(String[] args) {
        menu1();
        System.out.println("已退出系统");
    }
    //一级菜单
    public static void menu1(){
        while (true){
            System.out.println("某某某用户系统");
            System.out.println("**********");
            System.out.println("1.注册用户");
            System.out.println("2.登录用户");
            System.out.println("3.退出程序");
            System.out.println("**********");
            System.out.println("请输入你的选择:");
            int n = sc.nextInt();
            switch (n){
                case 1:register();break;
                case 2:login();break;
                case 3:return;
            }
        }
    }
    //二级菜单
    public static void menu2(){
        while (true){
            System.out.println("**************");
            System.out.println("1.展示用户信息");
            System.out.println("2.删除用户信息");
            System.out.println("3.返回上级菜单");
            System.out.println("**************");
            System.out.println("请输入你的选择:");
            int n = sc.nextInt();
            switch (n){
                case 1:show();break;
                case 2:delete();break;
                case 3:return;
            }
        }
    }
    //注册
    public static void register(){
        System.out.println("请输入用户名：");
        String name = sc.next();
        boolean flag = judgeName(name);
        if (!flag){
            System.out.println("请输入密码：");
            String password = sc.next();
            String[] phones = getPhones();
            System.out.println("请选择电话号码：1~9");
            int choice = sc.nextInt();
            String phone =phones[choice-1];
            System.out.println("电话号码选择成功！");
            users.add(new User(phone,password,name));
            System.out.println("注册成功！");
            output();
        }else System.out.println("用户名重复！");
    }
    //登录
    public static void login(){
        System.out.println("请输入用户名：");
        String name = sc.next();
        System.out.println("请输入密码：");
        String password = sc.next();
        boolean flag = false;
        for (int i = 0;i<users.size();i++){
            User u = users.get(i);
            if (u.getName().equals(name)){
                if (u.getPassword().equals(password)){
                    userIndex = i;
                    System.out.println("登录成功！");
                    flag = true;
                    break;
                }else {
                    System.out.println("密码错误！");
                }
            }else {
                System.out.println("用户名错误！");
            }
        }
        if (flag) {
            menu2();
        }
    }
    //展示
    public static void show(){
        for (int i = 0; i < users.size(); i++) {
            User u = users.get(i);
            System.out.println("编号："+(i+1)+" 用户名："+u.getName()
                    +" 密码："+u.getPassword()+" 电话号码："+u.getPhone());
        }
    }
    //删除
    public static void delete(){
        System.out.println("请输入需要删除信息的编号：");
        int index = sc.nextInt();
        boolean flag = false;
        for (int i = 0; i < users.size(); i++) {
            if (userIndex == index-1){
                System.out.println("不能删除当前登录账号！");
            }else if (i==index-1){
                users.remove(i);
                flag = true;
            }
        }
        if (flag) System.out.println("删除信息成功！");
        output();
    }
    //获取电话号码
    public static String[] getPhones(){
        String[] phones = new String[9];
        for (int i = 0; i < phones.length; i++) {
            String headPhone = "150";
            for (int j = 3; j < 11; j++) {
                int r = (int)(Math.random()*10);
                headPhone += r;
            }
            //检查生成的电话 是否与存在用户的电话冲突
            if (!checkPhone(headPhone)){
                phones[i] = headPhone;
            }else i--;
        }
        System.out.println("随机的九个电话号码：");
        for (int i = 0; i < phones.length; i++) {
            System.out.println(phones[i]);
        }
        return phones;
    }
    //判断电话
    public static boolean checkPhone(String phone){
        for (int i = 0; i < users.size(); i++) {
            User u = users.get(i);
            if (u.getPhone().equals(phone)){
                return true;
            }
        }
        return false;
    }
    //判断用户名是否重复
    public static boolean judgeName(String name){
        for (int i = 0; i < users.size(); i++) {
            User u = users.get(i);
            if (u.getName().equals(name)){
                return true;
            }
        }
        return false;
    }
    //序列化  写入
    public static void output(){
        try(FileOutputStream fos = new FileOutputStream("d://IO//newUser.xxx");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
        ) {
            oos.writeObject(users);
            System.out.println("集合写入成功！");
        }catch (IOException e){
            e.printStackTrace();
        }
    }
    //反序列  读取
    public static void input(){
        try (FileInputStream fis = new FileInputStream("d://IO//newUser.xxx");
             ObjectInputStream ois = new ObjectInputStream(fis);
        ){
            users =(List<User>)ois.readObject();
        }catch (FileNotFoundException e){
            System.out.println("欢迎第一次使用！");
        } catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
class User implements Serializable {
    private String phone;
    private String password;
    private String name;

    public User(String phone, String password, String name) {
        this.phone = phone;
        this.password = password;
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
